<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate"
          >&emsp;新增&emsp;
          </el-button
          >
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table
          ref="dataList"
          :data="menuList"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          border
          row-key="id"
          style="width: 100%; margin-bottom: 20px"
        >
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column label="序号" prop="id"></el-table-column>
          <el-table-column label="标题" prop="title"></el-table-column>
          <el-table-column label="标签" prop="label_id">
            <template slot-scope="{row}">
              <el-tag v-if="row.label_id === 1" type="success" size="normal"  effect="dark">扩列聊天</el-tag>
              <el-tag v-if="row.label_id === 2" size="normal"  effect="dark">休闲互动</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="房间分类id/游戏id" prop="type_id"></el-table-column>
          <el-table-column label="类型" prop="type">
            <template slot-scope="{row}">
              <el-tag v-if="row.type === 'room'" type="success" size="normal"  effect="dark">房间</el-tag>
              <el-tag v-if="row.type === 'game'" size="normal"  effect="dark">游戏</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status">
            <template slot-scope="{row}">
              <el-tag v-if="row.status === 0" type="warning" size="normal"  effect="dark">隐藏</el-tag>
              <el-tag v-if="row.status === 1" type="success" size="normal"  effect="dark">展示</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="图标" prop="icon">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img
                  :src="scope.row.icon"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.icon"
                  alt=""
                  style="width: 50px"
                />
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                plain
                type="primary"
                @click="handleUpdate(scope.row)"
              >编辑
              </el-button
              >
              <!-- <el-button
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                plain
                >删除</el-button
              > -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :limit.sync="listQuery.per_page"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    
    <!-- 新增编辑弹框 -->
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose('editRulesForm')"
    >
      <el-form
        ref="editRulesForm"
        :model="editRulesForm"
        :rules="editRulesFormRules"
        class="demo-ruleForm"
        label-width="120px"
      >
        <el-form-item label="名称" prop="title">
          <el-input
            v-model="editRulesForm.title"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="标签" prop="label_id">
          <el-select v-model="editRulesForm.label_id" placeholder="请选择" clearable filterable>
            <el-option v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="editRulesForm.type" placeholder="请选择" clearable filterable>
            <el-option v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="editRulesForm.type === 'room'" label="类型ID" prop="type_id">
          <el-select
              v-model="editRulesForm.type_id"
              filterable
              remote
              clearable
              reserve-keyword
              placeholder="请输入关键词"
              :remote-method="remoteMethod">
                  <el-option v-for="item in officialType"
                      :key="item.id"
                      :label="item.type_name"
                      :value="item.id">
                  </el-option>
          </el-select>
          
        </el-form-item>
        <el-form-item v-if="editRulesForm.type === 'game'" label="类型ID" prop="type_id">
          
          <el-select
              v-model="editRulesForm.type_id"
              filterable
              remote
              clearable
              reserve-keyword
              placeholder="请输入关键词"
              :remote-method="remoteMethod">
                  <el-option v-for="item in gameList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                  </el-option>
          </el-select>
          
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :before-upload="beforeAvatarUpload"
            :file-list="specialFlagBigList"

            :http-request="customizeUpload"
            :limit="1"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove"
            :on-success="handleSpecialFlagBigSuccess"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary"
            >上传图标
            </el-button
            >
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="editRulesForm.status">
            <el-radio :label="1">展示</el-radio>
            <el-radio :label="0">隐藏</el-radio>
          </el-radio-group>
          
        </el-form-item>

        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')"
          >提交
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { roomUserTypeList, userTypeSave, roomGameList,listData } from '@/api/roomType'

export default {
  data() {
    return {

      confirmBtn: false,
      options: [
        {
          value: 1,
          label: '扩列聊天'
        },
        {
          value: 2,
          label: '休闲互动'
        },
      ],
      options2: [
        {
          value: 'room',
          label: '房间'
        },
        {
          value: 'game',
          label: '游戏'
        },
      ],
      specialFlagBigList: [],
      // 要修改的当前行的id
      row_id: 0,
      // 选中的黑名单类别
      checkBlackList: [],
      // 表单规则
      editRulesFormRules: {
        title: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
        label_id: [{ required: true, message: '请选择', trigger: 'blur' }],
        type: [{ required: true, message: '请选择', trigger: 'blur' }],
        icon: [{ required: true, message: '请上传', trigger: 'blur' }],
        type_id: [{ required: true, message: '请选择', trigger: 'blur' }],
      },

      editRulesForm: {
        id: undefined,
        title: undefined,
        label_id: undefined,
        icon: undefined,
        type: undefined,
        type_id: undefined,
        status: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        per_page: 10
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: '',
      query: '',
      officialType: '',
      gameList: ''
    }
  },
  created() {
    this.getList()
    this.getOfficialType()
    this.getRoomGameList()
  },
  methods: {
    getOfficialType() {
      listData({
        page: 1,
        perPage: 1000
      }).then((response) => {
        this.officialType = response.data.list
      })
    },
    getRoomGameList() {
      roomGameList({
        page: 1,
        perPage: 1000
      }).then((response) => {
        this.gameList = response.data
      })
    },
    handleChangePic(fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/webp' || file.type === 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('请上传JPG/PNG格式的图片!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleSpecialFlagBigSuccess() {
      this.editRulesForm.icon = this.ossLastImgUrl
      this.specialFlagBigList = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove(res) {
      this.editRulesForm.icon = undefined
      this.specialFlagBigList = []
    },
    getList() {
      this.listLoading = true
      roomUserTypeList(this.listQuery).then((response) => {
        this.menuList = response.data.data
        this.total = response.data.total
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.editRulesForm = JSON.parse(JSON.stringify(row))
      this.specialFlagBigList = [{ url: this.editRulesForm.icon }]
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.editRulesForm = {
          id: undefined,
          title: undefined,
          label_id: undefined,
          icon: undefined,
          type: undefined,
          type_id: undefined,
          status: undefined
        }
      })
      this.specialFlagBigList = []
    },
    // 获取省份
    getProvince() {
    },
    // 获取城市
    getCity() {
    },
    submitForm(formName) {
      if (this.confirmBtn) return
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          userTypeSave(this.editRulesForm).then((response) => {
            if (response.code === 200) {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                type: 'success',
                message: '操作成功',
              })
            } else {
              this.confirmBtn = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    },
    remoteMethod(query) {
        this.query = query
        if (query !== '') {
            if(this.editRulesForm.type === 'room') {
                this.getOfficialType()
            }
            if(this.editRulesForm.type === 'game') {
                this.getRoomGameList()
            }
        }
    }
  }
}
</script>

<style scoped>
/deep/ .el-upload-list__item {
  transition : none !important;
}

/deep/ .el-upload-list--picture {
  max-height : 103px !important;
  overflow   : hidden;
}
</style>
